const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logs: [],
    arr: [],
    addresslist: [],
    region: ['省', '市', '区'],
    customItem: '全部',
    userName: "",
    phone: "",
    detail: "",
    isNewOne:1,//编辑处跳转，改变数据，新增地址跳转，新增一条数据
    isNewIndex:0,
    chooseaddr:1,
    nowbuydetail:{}
  },


  // 获取姓名
  userNameInput: function(e) {
    this.setData({
      userName: e.detail.value
    })
  },

  // 获取电话
  phoneInput: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 获取地址
  detailInput: function(e) {
    this.setData({
      detail: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let that = this
    that.setData({
      buynow:options.buynow,
      chooseaddr: options.chooseaddr
    })
    if (options) {
      if (options.data){
        console.log(options.data)
        let addrcurrent = JSON.parse(options.data)
        that.setData({
          region: addrcurrent.city,
          userName: addrcurrent.name,
          phone: addrcurrent.tel,
          detail: addrcurrent.detail
        })
      }
      if (options.detail){
        that.setData({
          nowbuydetail: JSON.parse(options.detail)
        })
      }
      console.log(that.data.detail)
      if (options.isNewOne){
        that.setData({
          isNewOne: options.isNewOne,
        })
      }
      if (options.isNewIndex){
        isNewIndex: options.index
      }
      console.log(that.data.isNewOne)
    }
    wx.getStorage({
      key: 'addresslist',
      success: function(res) {
        that.setData({
          arr: res.data
        })
      }
    })
  },

  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log(this.data.region)
    this.setData({
      region: e.detail.value
    })
    console.log(this.data.region)
  },

  saveAddress: function() {
    let that = this
    let phonetel = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/
    if (!that.data.userName) {
      wx.showToast({
        title: '收货人姓名不能为空',
        icon: 'none',
        mask: true
      })
    } else if (!phonetel.test(that.data.phone)) {
      wx.showToast({
        title: '手机号码格式不正确',
        icon: 'none',
        mask: true
      })
    } else if (!that.data.detail) {
      wx.showToast({
        title: '收货地址不能为空',
        icon: 'none',
        mask: true
      })
    } else {
      if(that.data.isNewOne==1){
        console.log(111)
        console.log(that.data.isNewOne)
        let addressItem = {
          'name': that.data.userName,
          'tel': that.data.phone,
          'city': that.data.region,
          'detailaddr': that.data.detail
        }
        that.data.arr.push(addressItem)
      } else if (that.data.isNewOne == 0){
        console.log(222)
        console.log(that.data.isNewOne)
        let arrZh = that.data.arr
        arrZh[that.data.isNewIndex].name = that.data.userName
        arrZh[that.data.isNewIndex].tel = that.data.phone
        arrZh[that.data.isNewIndex].city = that.data.region
        arrZh[that.data.isNewIndex].detailaddr = that.data.detail
        console.log(arrZh)
        that.setData({
          arr:arrZh
        })
      }
      console.log(that.data.arr)
      wx.setStorage({
        key: 'addresslist',
        data: that.data.arr,
      })
      wx.navigateTo({
        url: '/pages/addressList/addressList?chooseaddr=' + that.data.chooseaddr + '&buynow=' + that.data.buynow + '&isNewOne=' + that.data.isNewOne + '&detail=' + JSON.stringify(that.data.nowbuydetail),
      })
    }
  },
  //订单详情页回到首页
  backhome: function () {
    wx.switchTab({
      url: '../../pages/home/home',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})